package net.sevear.chaos.view.recommend
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	
	import net.sevear.chaos.ChaosTrackManager;
	import net.sevear.chaos.vo.UserInfo;
	
	import spark.components.HGroup;
	import spark.components.Label;
	
	public class UserQuickSlectViewItem extends HGroup
	{
		private var _selected:Boolean;
		private var _label:Label;
		private var _likeLbl:Label;
		private var _likeImg:Image;
		private var _user:UserInfo;
		private var _recentLikeId:int;
		private var _recentLikeName:String;
		private var onAirLbl:Label;
//		private var _color:uint;
		public function UserQuickSlectViewItem(width:int)
		{
			super();
			this.width = width;
			this.height = 20;
			this.verticalAlign = "center";
			_label = new Label();
			_label.setStyle("fontsize",14);
			_label.verticalCenter = 0;
			this.addElement(_label);
			
			onAirLbl = new Label();
			onAirLbl.setStyle("color",0x618C56);
			onAirLbl.visible = true;
			this.addElement(onAirLbl);
			
			_likeLbl = new Label();
			_likeLbl.setStyle("fontsize",12);
			_likeLbl.visible = false;
			this.addElement(_likeLbl);
			
			_likeImg = new Image();
			_likeImg.source = "/res/userlike.png";
			_likeImg.visible = false;
			
			
			
			_label.addEventListener(MouseEvent.CLICK,onClick);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			
			this.buttonMode = true;
			this.useHandCursor = true;
			_label.buttonMode = true;
			_label.useHandCursor = true;
			_label.setStyle("fontWeight","bold");
		}
		
		public function set selected(value:Boolean):void{
			if(_user==null && value == true) return ;
			else 
			{
				if(_selected!=value){
					_selected = value;
					_user.selected = value;
					dispatchEvent(new Event(Event.CHANGE));
					updateView();
				}
			}
		}
		
		public function get selected():Boolean{
			return _selected;
		}
		
		public function set user(value:UserInfo):void{
			if(value!=null){
				if(_user==null || _user.dbID != value.dbID){
					selected = false;
				}
				this.visible = true;
			}else{
				this.visible = false;
				selected = false;
			}
			_user = value;	
			updateView();
		}
		
		public function setRecentLikeTrackId(id:int,name:String):void{
			_recentLikeId = id;
			_recentLikeName = name;
			if(_user!=null && _recentLikeName!=null){
				_likeLbl.text = _recentLikeName;
				if(_likeLbl.hasEventListener(MouseEvent.CLICK)==false)
					_likeLbl.addEventListener(MouseEvent.CLICK,clickLikeLblHandler);
				if(this.containsElement(_likeImg)==false){
					this.addElementAt(_likeImg,2);
				}
			}else{
				_likeLbl.text = "";
				if(_likeLbl.hasEventListener(MouseEvent.CLICK))
					_likeLbl.removeEventListener(MouseEvent.CLICK,clickLikeLblHandler);
				if(this.containsElement(_likeImg)){
					this.removeElement(_likeImg);
				}
			}
		}
		
		public function getRecentLikeTrackId():int{
			return _recentLikeId;
		}
		
		public function get user():UserInfo{
			return _user;
		}
		
		public function setUserOnAir():void{
			onAirLbl.text = "on Air";
		}
		
		public function setUserNotOnAir():void{
			onAirLbl.text = "";
		}
		
		private function onClick(e:MouseEvent):void{
			if(selected) selected = false;
			else 
				selected = true;
		}
		
		private function updateView():void{
			if(_user!=null){
				_label.text = _user.dbName;
				if(selected==false){
					_label.setStyle("color",_user.color);
					_label.setStyle("backgroundAlpha",0);
				}
				else{
					_label.setStyle("backgroundAlpha",1);
					_label.setStyle("backgroundColor",_user.color);
					_label.setStyle("color",0xffffff);
				}
			}else{
				_label.text = "";
			}
		}
		
		private function onMouseOver(e:MouseEvent):void{
			_likeLbl.visible = true;
			_likeImg.visible = true;
			onAirLbl.visible = false;
		}
		
		private function onMouseOut(e:MouseEvent):void{
			_likeLbl.visible = false;
			_likeImg.visible = false;
			onAirLbl.visible = true;
		}
		
		private function clickLikeLblHandler(e:MouseEvent):void{
			ChaosTrackManager.getInstance().replay(_recentLikeId);
		}
	}
}